<?php
namespace app\index\controller;

use think\Db;


/**
 * Class Article
 * @package app\index\controller
 * @route('article')
 */
class Article extends Base
{
    public function table()
    {
        $this->assign('sort', ['type'=>'time','value'=>'id desc']);
        return $this->fetch();
    }

    public function tableHandle()
    {

        $pm = $this->params;
        $method = $pm['type'].'Table';
        $result = $this->logic()->article()->$method($pm);
        foreach ($result['list'] as &$item) {
            $item['create_date'] = date('Y-m-d H:i:s', $item['create_time']);
//            $tags = $this->logic()->tag()->getTagsHtmlByArticleId($item['id']);
            $item['url'] = url('detail',['id'=>$item['id']]);
        }
        $rt['code'] = 0;
        $rt['msg'] = '获取成功';
        $rt['result'] = $result;
        return json($rt);
    }

    public function tableHandle1()
    {
        $data = $this->params;
        $method = $data['type'].'Table';
        $list = $this->$method($data['type_value']);

        $append = '';
        foreach ($list as &$item) {
            $create_date = date('Y-m-d H:i:s', $item['create_time']);
            $tags = $this->logic()->tag()->getTagsHtmlByArticleId($item['id']);
            $url = url('detail',['id'=>$item['id']]);
            $append .=
                <<<html
                    <li class="chunk">
                        <h1><a href="{$url}">{$item['title']}</a></h1>
                        <p class="content">
                            {$item['describe']}
                        </p>
                        <p class="label">
								<span>
									<i class="fa fa-user-o"></i>
									cxcsz
								</span>
                                <span>
									<i class="fa fa-eye"></i>
									{$item['read_num']}
								</span>
                                <!--<span>
									<i class="fa fa-thumbs-o-up"></i>
									100
								</span>
                                <span>
									<i class="fa fa-comment-o"></i>
									100
								</span>-->
                                <span>
									<i class="fa fa-clock-o"></i>
									{$create_date}
								</span>
                        </p>
                        <p class="tags">
                            tags:
                            {$tags}
                        </p>
                    </li>

html;
        }
        $result['code'] = 0;
        $result['msg'] = '获取成功';
        $result['data']['append'] = $append;
        return json($result);
    }

    /**
     * @route('index/article/tag/:id')
     * ->pattern([id => '\d+'])
     */
    public function tag($id)
    {
        $this->assign('sort', ['type'=>'tag','value'=>$id]);
        return $this->fetch('table');
    }

    /**
     * @param $year
     * @param $month
     * @route('index/article/month/:year/:month')
     * ->pattern(['year'=>'\d{4}','month'=>'\d{2}'])
     */
    public function month($year, $month)
    {
        $min_date = $year.'-'.$month;
        $this->assign('sort', ['type'=>'month','value'=>$min_date]);
        return $this->fetch('table');
    }


    /**
     * @param $id
     * @route('index/article/category/:id')
     * ->pattern(['id'=>'\d+'])
     */
    public function category($id)
    {
        $this->assign('sort', ['type'=>'category', 'value'=>$id]);
        return $this->fetch('table');
    }

    public function detail()
    {
        $data = $this->params;
        if (empty($data['id'])) {
            return $this->error('参数id不能为空');
        }
        $where['id'] = $data['id'];
        $single = Db::name('article')->where($where)->find();
        if (!$single) {
            return $this->error('无当前id的文章');
        }
        $tags = $this->logic()->tag()->getTagsHtmlByArticleId($data['id']);
        $single['tags'] =$tags;
        $single['create_date'] = date('Y年m月d日 H:i:s', $single['create_time']);
        $last = Db::name('article')->field('id,title')->where('id', '<', $data['id'])->order('id desc')->find();
        if ($last) {
            $single['last'] = $last;
            $single['last']['url'] = url('index/article/detail',['id'=>$last['id']]);
        }
        $next = Db::name('article')->field('id,title')->where('id', '>', $data['id'])->find();
        if ($next) {
            $single['next'] = $next;
            $single['next']['url'] = url('index/article/detail',['id'=>$next['id']]);
        }
        $this->assign('data', $single);
        // 增加阅读数
        Db::name('article')->where($where)->setInc('read_num');
        return $this->fetch();
    }

}